// Copyright  2004-2008 Backplane Ltd.
//
// This file is part of Ubiquity formsPlayer
//
// Ubiquity formsPlayer is free software; you can redistribute it and/or modify it
// under the terms of the GNU General Public License as published by the
// Free Software Foundation; either version 3 of the License, or (at your
// option) any later version.
//
// Ubiquity formsPlayer is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
// FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
// more details.
//
// You should have received a copy of the GNU General Public License
// along with Ubiquity formsPlayer If not, see <http://www.gnu.org/licenses/>.
//	
//
//   Purpose: implementation of CXMLSender.
//   $Id$
#include "stdafx.h"
#include "xmlsender.h"

CXMLSender::CXMLSender(IXMLDOMNode * pTemplateNode,CImplModel * pModel)
	:m_pTemplateNode(pTemplateNode)
	,m_pModel(pModel)
{
	if(pTemplateNode)
		pTemplateNode->AddRef();
}

CXMLSender::~CXMLSender(void)
{
}

HRESULT CXMLSender::send(IXMLDOMNode * pNode)
{
	HRESULT hr = m_pRequest.CoCreateInstance(CLSID_XMLHTTPRequest);

	if(SUCCEEDED(hr))
	{
		makeURL(pNode);
		makeHeaders(pNode);
		sendBody(pNode);

	}	
	return hr;
}


HRESULT CXMLSender::makeURL(IXMLDOMNode * pNode)
{
	HRESULT hr = S_OK;
	
	return hr;
}
HRESULT CXMLSender::makeHeaders(IXMLDOMNode * pNode)
{
	HRESULT hr = S_OK;
	CComPtr<IXMLDOMNodeList> pNl;
	CComBSTR sDotDoubleSlashHeader(L".//header");
	m_pTemplateNode->selectNodes(sDotDoubleSlashHeader,&pNl);
	CComPtr<IXMLDOMNode> pNChild;
	pNl->nextNode(&pNChild);
	
	while(pNChild)
	{
		CComVariant vtName;
		CComQIPtr<IXMLDOMElement> pEl(pNChild);
		CComBSTR sAttrName(L"name");
		pEl->getAttribute(sAttrName,&vtName);
		CComBSTR sValue;
		CComVariant vtRef;
		sAttrName = L"ref";
		pEl->getAttribute(sAttrName,&vtRef);
		if(vtRef.bstrVal)
		{
		//	m_pModel->getInstanceNode(vtRef.bstrVal,pNode,false,&pResultNode);		
			//serialise the node
//			Serialize(pEl,pResultNode,&sValue);
		}
		else
		{
			pEl->get_text(&sValue);
		}

		//set the header with the given value.
		m_pRequest->setRequestHeader(vtName.bstrVal,sValue);


		//prepare next iteration
		pNChild.Release();
		pNl->nextNode(&pNChild);
	}


	return hr;

}
HRESULT CXMLSender::sendBody(IXMLDOMNode * pNode)
{
	HRESULT hr = S_OK;
	//Fetch the element that tells us how to format the body.
	CComPtr<IXMLDOMNode> pBody;
	CComBSTR sDotDoubleSlashBody(L".//body");
	m_pTemplateNode->selectSingleNode(sDotDoubleSlashBody,&pBody);
	CComQIPtr<IXMLDOMElement> pBodyEl(pBody);
	CComVariant vtRef;
	CComBSTR sRef(L"ref");
	hr = pBodyEl->getAttribute(sRef,&vtRef);
	CComBSTR sBody;
	if(S_OK == hr)
	{
		//Get the node referred to by the reef attribute
		CComPtr<IXMLDOMNode> pResultNode;
		m_pModel->getInstanceNode(vtRef.bstrVal,pNode,false,&pResultNode);
		//serialise it according to the appropriate serialisation.
//		Serialize(pBodyEl,pResultNode,&sBody);
	}
	else
	{
		//otherwise, body is just inline text.
		pBody->get_text(&sBody);
	}
	//Send it.
	m_pRequest->send(CComVariant(sBody));
	return hr;

}
/*
HRESULT Serialize(IXMLDOMElement * pTemplate, IXMLDOMNode *pNodeToSerialize,CComBSTR & bsRetval)
{


}
*/